package com.tgky.ybzb.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tgky.ybzb.entity.Jcybj;
import com.tgky.ybzb.entity.Rwal;
import com.tgky.ybzb.service.IRwalService;
import com.tgky.ybzb.utils.TimeConvert;
import com.tgky.ybzb.utils.excel.ExcelUtils;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import com.tgky.ybzb.entity.Rwalj;
import com.tgky.ybzb.service.IRwaljService;
import com.tgky.ybzb.common.IdParam;
import com.tgky.ybzb.common.PageParam;
import com.tgky.ybzb.common.Result;
import com.tgky.ybzb.utils.MPUtil;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.tgky.ybzb.constant.ResultConstant.ERROR_500;

/**
 * 任务案例集 前端控制器
 *
 * @author XYP
 * @since 2024-07-22
 */
@RestController
@AllArgsConstructor
@Slf4j
@Api(value = "任务案例集", tags = "任务案例集接口")
@RequestMapping("rwalj")
public class RwaljController {


    @Resource
    private IRwaljService rwaljService;


    @Resource
    private IRwalService rwalService;

    @PostMapping("/import")
    @ApiOperation(value = "excel导入")
    public Result excelImport(MultipartFile file,@RequestParam(required = true) String type) {

        try {
            String originalFilename = file.getOriginalFilename();
            String[] rwaljData = originalFilename.substring(0, originalFilename.lastIndexOf(".")).split("-");
            if (rwaljData.length != 4) {
                return Result.error("任务案例集文件名不符合规范！");
            }

            //解析任务案例集合数据
            List<Rwal> rwalList = ExcelUtils.readMultipartFile(file, Rwal.class);

            Rwalj rwalj = new Rwalj(rwaljData[0], rwaljData[1], rwaljData[2], rwaljData[3], rwalList.size());
            rwalj.setCreateTime(TimeConvert.getCurrentDateTime());
            //当类型为1则解析文件，为2则保存文件数据
            if(type!=null && !"1".equals(type)) {
                //存储任务案例集信息
                rwaljService.save(rwalj);
            }

            if (rwalList != null && rwalList.size() > 0) {
                //存储关联任务案例信息
                rwalList.stream().forEach(rwal -> {
                    rwal.setSsjcybjID(rwalj.getId());
                    rwal.setCreateTime(TimeConvert.getCurrentDateTime());
                });
                //当类型为1则解析文件，为2则保存文件数据
                if(type!=null && !"1".equals(type)) {
                    rwalService.saveBatch(rwalList);
                }
            }
            rwalj.setRwalList(rwalList);
            if(type!=null && "1".equals(type)){
                return Result.successData(rwalj);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(ERROR_500, "系统错误");
        }
        return Result.success("任务案例集数据导入成功");
    }


    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "新增", notes = "传入rwalj")
    public Result save(@RequestBody Rwalj rwalj) {
        if (rwalj == null) {
            return Result.error("参数异常");
        }
        rwalj.setCreateTime(TimeConvert.getCurrentDateTime());
        return Result.status(rwaljService.save(rwalj));
    }

    /**
     * 修改 任务案例集
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "修改", notes = "传入rwalj")
    public Result update(@RequestBody Rwalj rwalj) {
        if (rwalj == null) {
            return Result.error("参数异常");
        }
        return Result.status(rwaljService.updateById(rwalj));
    }

    /**
     * 删除 任务案例集
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "删除", notes = "传入ids")
    public Result remove(@RequestBody IdParam<Long> idParam) {
        if (idParam == null) {
            return Result.error("参数异常！");
        }
        // 删除案例
        for(Long mid:idParam.getIds()){
            Map<String,Object> map=new HashMap();
            map.put("ssjcybjID",mid);
            rwalService.removeByMap(map);
        }
        // 删除案例集
        return Result.status(rwaljService.removeByIds(idParam.getIds()));
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "详情", notes = "传入id")
    public Result<Rwalj> detail(Long id) {
        if (id == null) {
            return Result.error("参数异常");
        }
        Rwalj detail = rwaljService.getById(id);
        return Result.successData(detail);
    }

    /**
     * 查询 任务案例集
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "查询", notes = "传入rwalj")
    public Result list(PageParam pageParam, Rwalj rwalj) {
        List<Rwalj> list = rwaljService.list(rwaljService.getQueryWrapper(pageParam, rwalj));
        return Result.successData(list);
    }

    /**
     * 分页 任务案例集
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "分页", notes = "传入rwalj")
    public Result<IPage> page(PageParam pageParam, Rwalj rwalj) {
        IPage<Rwalj> pages=rwaljService.getRwaljListAndJcyb(MPUtil.getPage(pageParam),rwalj);
        return Result.successData(pages);
    }

}
